package crawl

import (
	"encoding/json"
	"encoding/xml"
	"io"
	"net/http"
)

type Processor = func(resp *http.Response) error

func ProcessBytes(readBytes func(data []byte) error) Processor {
	return func(resp *http.Response) error {
		data, err := io.ReadAll(resp.Body)
		if err == nil {
			err = readBytes(data)
		}
		return err
	}
}

func ProcessJSON(out interface{}) Processor {
	return ProcessBytes(func(data []byte) error {
		return json.Unmarshal(data, out)
	})
}

func ProcessXML(out interface{}) Processor {
	return ProcessBytes(func(data []byte) error {
		return xml.Unmarshal(data, out)
	})
}
